#!/usr/bin/env perl
require 5.008;
use warnings;
use strict;

unshift(@INC, '.');
require qpdf_test_helpers;

chdir("qpdf") or die "chdir testdir failed: $!\n";

require TestDriver;

cleanup();

my $td = new TestDriver('specific-file');

my $n_tests = 11;

# Special PDF files that caused problems at some point

$td->runtest("damaged stream",
             {$td->COMMAND => "qpdf --check damaged-stream.pdf"},
             {$td->FILE => "damaged-stream.out", $td->EXIT_STATUS => 3},
             $td->NORMALIZE_NEWLINES);
$td->runtest("damaged stream (C)",
             {$td->COMMAND => "qpdf-ctest 2 damaged-stream.pdf '' a.pdf"},
             {$td->FILE => "damaged-stream-c-check.out", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("compress objstm and xref",
             {$td->COMMAND =>
                  "qpdf --static-id --stream-data=compress".
                  " --object-streams=generate minimal.pdf a.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check output",
             {$td->COMMAND => "qpdf-test-compare a.pdf compress-objstm-xref.pdf"},
             {$td->FILE => "compress-objstm-xref.pdf", $td->EXIT_STATUS => 0});
$td->runtest("qdf + preserved-unreferenced + xref streams",
             {$td->COMMAND => "qpdf --qdf --preserve-unreferenced" .
                  " --static-id compress-objstm-xref.pdf a.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("check output",
             {$td->COMMAND => "qpdf-test-compare a.pdf compress-objstm-xref-qdf.pdf"},
             {$td->FILE => "compress-objstm-xref-qdf.pdf", $td->EXIT_STATUS => 0});
$td->runtest("check fix-qdf idempotency",
             {$td->COMMAND => "fix-qdf a.pdf"},
             {$td->FILE => "a.pdf", $td->EXIT_STATUS => 0});
$td->runtest("pages points to page",
             {$td->COMMAND =>
                  "qpdf --static-id --linearize --compress-streams=n" .
                  " pages-is-page.pdf a.pdf"},
             {$td->FILE => "pages-is-page.out", $td->EXIT_STATUS => 3},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check output",
             {$td->FILE => "a.pdf"},
             {$td->FILE => "pages-is-page-out.pdf"});
$td->runtest("Acroform /DR with indirect subkey",
             {$td->COMMAND =>
                  "qpdf --static-id --empty" .
                  " --pages dr-with-indirect-item.pdf -- a.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check output",
             {$td->FILE => "a.pdf"},
             {$td->FILE => "dr-with-indirect-item-out.pdf"});

cleanup();
$td->report($n_tests);
